package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.CartVO;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class CartServiceImpl implements CartService {

    @Reference
    private ItemService itemService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<CartVO> insertCart(List<CartVO> cartList, Long itemId, Integer num) {
        List<CartVO> list = null;
        //1.根据商家查询item 商品
        TbItem tbItem = itemService.selectById(itemId);
        //2.获取商家的sellerID;
        String sellerId = tbItem.getSellerId();
        //3.通过(商家ID)sellerId 来判断 原来的购物车是否有该商家的购物车数据
        CartVO cart = findCartBySellerId(cartList, sellerId);
        //4.如果没有
        if (cart == null){
            //4.1 创建一个cart 对象
            cart = new CartVO();
            //将商家的id 放进去
            cart.setSellerId(sellerId);
            //4.2 将我们要添加的商品放到cart对象中itemList 里面
            List<TbOrderItem> tbOrderItems = new ArrayList<>();
            TbOrderItem tbOrderItem = new TbOrderItem();
            //4.3放入购物车对象中
            list = setCart(cartList, num, tbItem, cart, tbOrderItems, tbOrderItem);
        }else {//5有
            List<TbOrderItem> tbOrderItemList = cart.getTbOrderItemList();
            // 5.1 判断购物车里 有没有明细
            TbOrderItem orderItemIdByItemId = findOrderItemIdByItemId(tbItem, tbOrderItemList);
            // 5.2 如果没有 创建明细对象，然后再追加一条
            if (orderItemIdByItemId == null){
                orderItemIdByItemId = new TbOrderItem();
                setCart(cartList, num, tbItem, cart, tbOrderItemList, orderItemIdByItemId);
                //将 创建好的对象放到明细列表中
                tbOrderItemList.add(orderItemIdByItemId);//引用传递
//                cart.setTbOrderItemList(tbOrderItemList); 多余
            }else{
                //5.1.2 如果有明细对象那么 就在原来num 上 + 1 即可
                orderItemIdByItemId.setNum(tbItem.getNum() + num);
                orderItemIdByItemId.setPrice(new BigDecimal(tbItem.getPrice().doubleValue() * orderItemIdByItemId.getNum()));
                //如果是减的话 当商品的数量 <= 0 时，将商品从明细列表中移除掉
                if (orderItemIdByItemId.getNum().intValue() == 0){
                    tbOrderItemList.remove(orderItemIdByItemId);
                }
                //如果明细中的num为0 了那么就将商家也移除出去
                if(tbOrderItemList.size() <= 0){
                    cartList.remove(cart);
                }
            }


        }
        return list;
    }



    private TbOrderItem findOrderItemIdByItemId(TbItem tbItem, List<TbOrderItem> tbOrderItemList) {
        for (TbOrderItem tbOrderItem : tbOrderItemList) {
            if (tbOrderItem.getSellerId().equals(tbItem.getId())){
                return tbOrderItem;
            }
        }
        return null;
    }


    //放入购物车对象中
    private List<CartVO>  setCart(List<CartVO> cartList, Integer num, TbItem tbItem, CartVO cart, List<TbOrderItem> tbOrderItems, TbOrderItem tbOrderItem) {
        tbOrderItem.setItemId(tbItem.getId());
        tbOrderItem.setGoodsId(tbItem.getGoodsId());
        tbOrderItem.setTitle(tbItem.getTitle());
        tbOrderItem.setNum(tbItem.getNum());
        tbOrderItem.setPrice(tbItem.getPrice());
        tbOrderItem.setPicPath(tbItem.getImage());
        tbOrderItem.setSellerId(tbItem.getSellerId());
        tbOrderItem.setTotalFee(new BigDecimal(tbItem.getPrice().doubleValue() * num));
        //放到明细列表list
        tbOrderItems.add(tbOrderItem);
        //放到购物车对象cart
        cart.setTbOrderItemList(tbOrderItems);
        //购物车对象放到购物车列表中
        cartList.add(cart);
        return cartList;
    }

    private CartVO findCartBySellerId(List<CartVO> cartList, String sellerId) {
        for (CartVO cart : cartList) {
            if (Objects.equals(cart.getSellerId(), sellerId)){
                return cart;
            }
        }
        return null;
    }

    @Override
    public void addCartListToRedis(String username, List<CartVO> cartList) {
        redisTemplate.boundHashOps("cartList").put(username,cartList);
    }

    @Override
    public List<CartVO> findListCartFromRedis(String username) {
        List<CartVO> cartList = (List<CartVO>)redisTemplate.boundHashOps("cartList").get(username);
        if (cartList == null){
            cartList = new ArrayList<>();
        }
        return cartList;
    }

    //合并购物车
    @Override
    public List<CartVO> merageCartList(List<CartVO> cookieList, List<CartVO> redisList) {
        for (CartVO cart : cookieList) {
            List<TbOrderItem> tbOrderItemList = cart.getTbOrderItemList();
            for (TbOrderItem tbOrderItem : tbOrderItemList) {
                Long itemId = tbOrderItem.getItemId();
                Integer num = tbOrderItem.getNum();
                redisList = insertCart(redisList, itemId, num);
            }
        }
        return redisList;
    }
}

